package com.chat.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: WFP
 * @Time: 2017/12/30
 * @decription:
 */
public class MessageServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("***Servlet初始化***");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取得表单参数
        String[] userName = req.getParameterValues("users");
        String message = req.getParameter("message");

        if (userName!=null){
            //私聊
            ServletContext application = this.getServletContext();// 获取application
            Map<String,HttpSession> m = (Map<String, HttpSession>) application.getAttribute("allmap");
            for(String name : userName){
                saveMessage(req, message, m, name);
            }
        }else {
            //公聊
            ServletContext application = this.getServletContext();// 获取application
            Map<String,HttpSession> m = (Map<String, HttpSession>) application.getAttribute("allmap");
            List<String> users = (List) application.getAttribute("alluser");
            for(String name : users){
                saveMessage(req, message, m, name);
            }
        }
        resp.sendRedirect("chat.jsp");
    }

    /**
     * 将message保存到session中
     * @param req httprequest
     * @param message 消息
     * @param m 存放"用户名--->HttpSession"键值对的map
     * @param name 需要接收消息的用户的名称
     */
    private void saveMessage(HttpServletRequest req, String message, Map<String, HttpSession> m, String name) {
        if(req.getSession().getAttribute("uname").equals(name)) {

        }else {
            //取出历史消息记录
            List<String> messageList = (List<String>) m.get(name).getAttribute("message");
            if (messageList != null) {
                messageList.add(req.getSession().getAttribute("uname") + "  对  " + name + "  说: " + message);
                m.get(name).setAttribute("message", messageList);
            } else {
                List<String> messageListFirst = new ArrayList<>();
                messageListFirst.add(req.getSession().getAttribute("uname") + "  对  " + name + "  说: " + message);
                m.get(name).setAttribute("message", messageListFirst);
            }

            messageList = (List<String>) m.get(req.getSession().getAttribute("uname")).getAttribute("message");
            if (messageList != null) {
                messageList.add(req.getSession().getAttribute("uname") + "  对  " + name + "  说: " + message);
                m.get(req.getSession().getAttribute("uname")).setAttribute("message", messageList);
            } else {
                List<String> messageListFirst = new ArrayList<>();
                messageListFirst.add(req.getSession().getAttribute("uname") + "  对  " + name + "  说: " + message);
                m.get(req.getSession().getAttribute("uname")).setAttribute("message", messageListFirst);
            }
        }
    }

    @Override
    public void destroy() {
        System.out.println("***Servlet销毁***");
    }
}
